package com.wang.luntan.users.head.controller;

import com.wang.luntan.common.pojo.ApiResponse;
import com.wang.luntan.common.pojo.ApiResponseEnum;
import com.wang.luntan.common.util.ConstatFinalUtil;
import com.wang.luntan.common.util.PageInfoUtil;
import com.wang.luntan.common.web.controller.BaseController;
import com.wang.luntan.users.pojo.AtOpinionDesc;
import com.wang.luntan.users.pojo.AtOpinionType;
import com.wang.luntan.users.pojo.AtUsers;
import com.wang.luntan.users.service.AtOpinionDescService;
import com.wang.luntan.users.service.AtOpinionTypeService;
import com.wang.luntan.users.service.IOpinionDbService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户首页操作
 *
 * @author wangsh
 */
@Log4j2
@Controller
@RequestMapping("/head/opinionDesc")
public class OpinionDescHeadController extends BaseController {

    private String STR_PREFIX = "opinionDesc";

    @Autowired
    private IOpinionDbService opinionDescService;

    @Autowired
    private AtOpinionDescService atOpinionDescService;


    @Autowired
    private AtOpinionTypeService opinionTypeService;
    
    /**
     * 首页
     *
     * @return
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request, Model model) {
        log.info("---{}-list---", STR_PREFIX);
        /* 分页信息 */
        PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
        String parentId = request.getParameter("parentId");
        parentId = StringUtils.isBlank(parentId) ? "0" : parentId;

        Map<String,Object> paramsMap = this.proccedSearch(request);
        paramsMap.put("parentId", parentId);
        ApiResponse<AtOpinionDesc> apiResponse = this.opinionDescService.findCondListOpinionDescService(pageInfoUtil, paramsMap);

        apiResponse.clearInfo();
        model.addAttribute("response", apiResponse.toJSON());
        model.addAttribute("parentId", parentId);
        return "/head/comment/opinionDescList";
    }

    @RequestMapping("/add")
    public String add(HttpServletRequest request, Model model) {
        log.info("---{}-add---", STR_PREFIX);
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        List<AtOpinionType> opinionTypeList = this.opinionTypeService.getList(paramsMap);

        ApiResponse<AtOpinionType> apiResponse = new ApiResponse<AtOpinionType>();
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        Map<String, List<AtOpinionType>> dataList = new HashMap<String, List<AtOpinionType>>();

        /* 将结果放到JSON中 */
        dataList.put(ApiResponseEnum.NAME_LIST.getName(), opinionTypeList);
        apiResponse.setDataListJava(opinionTypeList);
        apiResponse.setDataList(dataList);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);

        model.addAttribute("opinionTypeList", apiResponse.toJSON());


        log.info("---{}-list---", STR_PREFIX);
        String parentId = request.getParameter("parentId");
        parentId = StringUtils.isBlank(parentId) ? "0" : parentId;

        Map<String, Object> paramsDescMap = new HashMap<String, Object>();
        paramsDescMap.put("parentId", parentId);
        List<AtOpinionDesc> opinionDescList = this.atOpinionDescService.getList(paramsDescMap);

        ApiResponse<AtOpinionDesc> apiResponseDesc = new ApiResponse<AtOpinionDesc>();
        apiResponseDesc.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        Map<String, List<AtOpinionDesc>> dataListDesc = new HashMap<String, List<AtOpinionDesc>>();

        /* 将结果放到JSON中 */
        dataListDesc.put(ApiResponseEnum.NAME_LIST.getName(), opinionDescList);
        apiResponseDesc.setDataListJava(opinionDescList);
        apiResponseDesc.setDataList(dataListDesc);
        apiResponseDesc.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponseDesc.getCode() + ""), Collections.EMPTY_MAP);

        model.addAttribute("opinionDescList", apiResponseDesc.toJSON());
        return "/head/comment/opinionDescAdd";
    }

    @RequestMapping("/addSubmit")
    public String addSubmit(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        AtUsers users = (AtUsers) session.getAttribute("users");

        String typeId = request.getParameter("typeId");
        String parentId = request.getParameter("parentId");
        String name = request.getParameter("name");
        String content = request.getParameter("content");
        String status = request.getParameter("status");

        Date date = new Date();
        AtOpinionDesc desc = new AtOpinionDesc();
        desc.setTypeId(Integer.parseInt(typeId));
        desc.setParentId(Integer.parseInt(parentId));
        desc.setName(name);
        desc.setContent(content);
        desc.setStatus(Byte.parseByte(status));

//        desc.setStatus(Byte.parseByte(AtAdminsEnum.STATUS_ENABLE.getStatus() + "") );
//        desc.setStatus(Byte.parseByte(AtAdminsEnum..getStatus() + "") );
        desc.setCreateId(users.getId());
        desc.setCreateTime(date);
        desc.setUpdateTime(date);
        desc.setPubTime(date);
        ApiResponse<Object> objectApiResponse = this.opinionDescService.saveOneOpinionDescService(desc);
        return this.list(request, model);
    }

    @RequestMapping("/descInfo")
    public String descInfo(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");

        Map<String, Object> paramsDescMap = new HashMap<String, Object>();
        paramsDescMap.put("id", id);
        ApiResponse<AtOpinionDesc> atOpinionDescApiResponse = this.opinionDescService.findOneOpinionDescService(paramsDescMap);
        model.addAttribute("atOpinionDescApiResponse", atOpinionDescApiResponse.toJSON());
        return "/head/comment/opinionDescInfo";
    }

    @RequestMapping("/descDel")
    public String descDel(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        String parentId = request.getParameter("parentId");
        request.setAttribute("parentId", parentId);

        Map<String, Object> paramsDescMap = new HashMap<String, Object>();
        paramsDescMap.put("id", id);
        ApiResponse<Object> objectApiResponse = this.opinionDescService.deleteOneOpinionDescService(paramsDescMap);
        model.addAttribute("objectApiResponse", objectApiResponse.toJSON());
        return this.list(request, model);
    }

    @RequestMapping("/edit")
    public String edit(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");

        Map<String, Object> paramsInfoMap = new HashMap<String, Object>();
        paramsInfoMap.put("id", id);
        ApiResponse<AtOpinionDesc> atOpinionDescApiResponse = this.opinionDescService.findOneOpinionDescService(paramsInfoMap);
        model.addAttribute("atOpinionDescApiResponse", atOpinionDescApiResponse.toJSON());

        log.info("---{}-edit---", STR_PREFIX);
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        List<AtOpinionType> opinionTypeList = this.opinionTypeService.getList(paramsMap);

        ApiResponse<AtOpinionType> apiResponse = new ApiResponse<AtOpinionType>();
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        Map<String, List<AtOpinionType>> dataList = new HashMap<String, List<AtOpinionType>>();

        /* 将结果放到JSON中 */
        dataList.put(ApiResponseEnum.NAME_LIST.getName(), opinionTypeList);
        apiResponse.setDataListJava(opinionTypeList);
        apiResponse.setDataList(dataList);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);

        model.addAttribute("opinionTypeList", apiResponse.toJSON());


        log.info("---{}-list---", STR_PREFIX);
        String parentId = request.getParameter("parentId");
        parentId = StringUtils.isBlank(parentId) ? "0" : parentId;

        Map<String, Object> paramsDescMap = new HashMap<String, Object>();
        paramsDescMap.put("parentId", parentId);
        List<AtOpinionDesc> opinionDescList = this.atOpinionDescService.getList(paramsDescMap);

        ApiResponse<AtOpinionDesc> apiResponseDesc = new ApiResponse<AtOpinionDesc>();
        apiResponseDesc.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        Map<String, List<AtOpinionDesc>> dataListDesc = new HashMap<String, List<AtOpinionDesc>>();

        /* 将结果放到JSON中 */
        dataListDesc.put(ApiResponseEnum.NAME_LIST.getName(), opinionDescList);
        apiResponseDesc.setDataListJava(opinionDescList);
        apiResponseDesc.setDataList(dataListDesc);
        apiResponseDesc.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponseDesc.getCode() + ""), Collections.EMPTY_MAP);

        model.addAttribute("opinionDescList", apiResponseDesc.toJSON());
        return "/head/comment/opinionDescEdit";
    }

    @RequestMapping("/editSubmit")
    public String editSubmit(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        String typeId = request.getParameter("typeId");
        String parentId = request.getParameter("parentId");
        String name = request.getParameter("name");
        String content = request.getParameter("content");
        String status = request.getParameter("status");

        Date date = new Date();
        AtOpinionDesc desc = new AtOpinionDesc();
        desc.setId(Integer.parseInt(id));
        desc.setTypeId(Integer.parseInt(typeId));
        desc.setParentId(Integer.parseInt(parentId));
        desc.setName(name);
        desc.setContent(content);
        desc.setStatus(Byte.parseByte(status));

//        desc.setStatus(Byte.parseByte(AtAdminsEnum.STATUS_ENABLE.getStatus() + "") );
//        desc.setStatus(Byte.parseByte(AtAdminsEnum..getStatus() + "") );
//        desc.setCreateTime(date);
        desc.setUpdateTime(date);
//        desc.setPubTime(date);
        ApiResponse<Object> apiResponse = this.opinionDescService.updateOneOpinionDescService(desc);
        model.addAttribute("response", apiResponse);
        if (apiResponse.getCode() == ApiResponseEnum.STATUS_SUCCESS.getStatus()) {
            /* 成功 */
            return "redirect:/head/opinionDesc/list";
        }
        return this.edit(request, model);
    }
}
